from .base import Search


class Binary(Search):
    DEBUG = 1

    def search(self, key, data):
        '''
        data:升序有序序列
        '''
        low = 0
        hight = len(data)-1
        i = 0
        while low <= hight:
            i += 1
            middle = (hight+low)//2
            if self.DEBUG > 1:
                print(f"{middle}:{data[middle]} {key}")
            if data[middle] > key:
                hight = middle-1
            elif data[middle] < key:
                low = middle+1
            else:
                if self.DEBUG > 0:
                    print(f"命中次数：{i} \n数据总数{len(data)}")
                return middle
        return -1
